package com.media.themovieworld.common.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.media.themovieworld.R;
import com.media.themovieworld.api.ApiParams;
import com.media.themovieworld.model.MovieItemModel;
import com.media.themovieworld.util.CommonUtil;
import com.media.themovieworld.util.MovieImageLoader;
import com.media.themovieworld.widget.ViewHolder;

public class MovieAdapter extends BaseListAdapter<MovieItemModel>{

	private int imageWidth;
	private int imageHeight;
	
	public MovieAdapter(Activity activity, ArrayList<MovieItemModel> datas) {
		super(activity, datas);
		imageWidth = mActivity.getResources().getDimensionPixelSize(R.dimen.movie_image_width);
		imageHeight = mActivity.getResources().getDimensionPixelSize(R.dimen.movie_image_height);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			row = LayoutInflater.from(mActivity).inflate(R.layout.movie_data_row, parent,false);
		}
		ImageView imgPoster = ViewHolder.get(row, R.id.tmw__mdr_poster);
		ImageView imgNew = ViewHolder.get(row, R.id.tmw__mdr_iNew);
		TextView tMovieName = ViewHolder.get(row, R.id.tmw__mdr_tName);
		TextView tMovieActor = ViewHolder.get(row, R.id.tmw__mdr_tDienvien);
		TextView tDuration = ViewHolder.get(row, R.id.tmw__mdr_tDuration);
		RatingBar ratingBar = ViewHolder.get(row, R.id.tmw__mdr_rRating);
		TextView tPrice = ViewHolder.get(row, R.id.tmw__mdr_tView);
		
		MovieItemModel model = getItem(position);
		tMovieName.setText(model.name);
		tMovieActor.setText(model.actor);
		tDuration.setText(CommonUtil.convertToTimeString(model.showTime));
		if (model.isNew == 0) {
			imgNew.setVisibility(View.GONE);
		}else{
			imgNew.setVisibility(View.VISIBLE);
		}
		
		String url = ApiParams.getDownloadImageLink(imageWidth, imageHeight, model.icon);
		MovieImageLoader.getInstance().displayImage(url, imgPoster, MovieImageLoader.getDisplayImageOptionDefault());
		return row;
	}

}
